package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.JsonRequest;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController {

    @Autowired
    private IEmployeeService iEmployeeService;
    @Autowired
    private IPermissionService iPermissionService;

    @RequestMapping("/empLogin")
    @ResponseBody
   public JsonRequest login(String username, String password, HttpSession session){
            JsonRequest jsonRequest=new JsonRequest();
            Employee employee = iEmployeeService.selectOne(username, password);
            UserContext.setUser(employee);
            List<String> list= iPermissionService.selectPermission(employee.getId());
            UserContext.setPermission(list);
            return jsonRequest;
   }

   @RequestMapping("/empLogout")
   public String logout(HttpSession session){
        session.invalidate();//把session设置为无效
//        session.removeAttribute("EMPLOYEE_IN_SESSIOM");
        return "redirect:/login.html";
   }

   @RequestMapping("/empnointerceptor")
    public String noInterceptro(){
        return "common/nopermission";
   }

   @RequestMapping("/erro")
    public String erroException(){
        return "common/error";
   }

}
